import smtplib
from email.mime.text import MIMEText


class SendEMail(object):
    def __init__(self, host: str, port: int, user: str, pwd: str):
        self.host = host
        self.port = port
        self.user = user
        self.pwd = pwd

    # mail_host = "smtp.qq.com"  # 服务器：imap/smtp
    # Port_number = 465  # SSL端口：465或587或25（无协议）
    # mail_user = "2085195880@qq.com"  # 邮箱用户名
    # mail_pwd = "oozhbxrpwbsmbfed"  # 授权码
    # sender = "2085195880@qq.com"  # 发送者邮箱账号名
    # receivers = [sender, "2085195880@qq.com"]  # 接收者

    def send_mail(self, text, subject, sender, receivers):
        message = MIMEText(text, "plain", "utf-8")  # 文本信息
        message['From'] = sender
        message['To'] = receivers[0]
        message['Subject'] = subject

        try:
            smtpObj = smtplib.SMTP()
            smtpObj.connect(self.host, self.port)
            smtpObj.login(self.user, self.pwd)  # 登录
            smtpObj.sendmail(sender, receivers, message.as_string())  # 发送人，接收人，文本信息
            smtpObj.quit()
            print("邮件发送成功")
            return True
        except smtplib.SMTPException as e:
            print("Error：无法发送邮件")
            print(e)
            return False


if __name__ == "__main__":
    SendEmail = SendEMail(host="smtp.qq.com", port=25, user="2085195880@qq.com", pwd="oozhbxrpwbsmbfed")
    result = SendEmail.send_mail(text="正文", subject="主题", sender="2085195880@qq.com", receivers="2085195880@qq.com")
